pub mod cbase;
pub mod md;
pub mod query;
pub mod queue;
pub mod util;
pub mod write;

use super::{get_db, Client, MapSv, VMap, VRece, VSend};
use base::{
    base::{Base, VBase},
    err, err_value, ok, throw_err, ECode, Resp,
};
use serde_json::{json, Value};

pub async fn doit(data: &Value, cmd: &str, client: &Client, tx: VSend) -> Resp {
    let data = data
        .as_object()
        .ok_or(format!("data format wrong: {:?}.", data))?;

    match cmd {
        "put" | "chunk" | "gput" | "gchunk" | "vput" | "vchunk" => {
            let v = write::Write::new(&client);
            v.do_cmd(cmd, data, tx).await
        }
        "rm" | "md" | "mdchunk" => {
            let v = md::Md;
            v.do_cmd(cmd, data, &client).await
        }
        "get" => {
            let v = query::Query;
            v.do_cmd(data, &client).await
        }
        "rmtab" | "tab" | "field" | "tag" | "col" => {
            let v = util::Util::new(&client);
            v.do_cmd(cmd, data).await
        }
        _ => throw_err!(ECode::NoServe, "no serve"),
    }
}
